using YMJake.Flowable.ExternalWorker.Client.Core.Rest;
using YMJake.Flowable.ExternalWorker.Client.Core.Variables;

namespace YMJake.Flowable.ExternalWorker.Client.Core.Extensions;

/// <summary>
/// Represents a mutable collection of variables that can be converted into Flowable REST variables.
/// </summary>
public sealed class VariableExtensions
{
    private readonly Dictionary<string, VariableBase> _variables = new(StringComparer.Ordinal);

    public void Set(string name, VariableBase variable)
    {
        _variables[name] = variable ?? throw new ArgumentNullException(nameof(variable));
    }
    public void Set(string name, string value) => Set(name, new StringVariable(value));
    public void Set(string name, int value) => Set(name, new IntegerVariable(value));
    public void Set(string name, long value) => Set(name, new LongVariable(value));
    public void Set(string name, double value) => Set(name, new DoubleVariable(value));
    public void Set(string name, bool value) => Set(name, new BooleanVariable(value));
    public void Set(string name, DateTime value) => Set(name, new DateVariable(value));
    public void Set(string name, DateOnly value) => Set(name, new DateOnlyVariable(value));
    public void SetJson<T>(string name, T value) => Set(name, JsonVariable.Create(value));

    public List<EngineRestVariable> ToEngineRestVariables()
    {
        return _variables.Select(kvp => new EngineRestVariable(kvp.Key, kvp.Value.TypeName, kvp.Value.RawValue)).ToList();
    }

    public bool Any() => _variables.Count > 0;
}
